<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
class Slide extends Controller
{
    public function index()
    {
        $res = Db::name('slide')->select();
        // dump($res);
        // exit;
        $this->assign('res',$res);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function add1()
    {
        $request = Request::instance();
        $post = $request->post();
        // dump($post);
        // exit;
        $file = request()->file('pic');
        if($file)
        {
            $info = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'img');
            if($info)
            {
                $pic = $info->getFilename(); 
            }
        }
        $request = Request::instance();
        $http = $request->domain();
        $src = "$http/zyd/public/static/img/$pic";
        $data=[
            'src'=>$_POST['src'],
            'pic'=> $src,
        ];
        // dump($data);
        // exit;
       $hd = Db::table('slide')->insert($data);
       if($hd)
            {
                return $this->success('添加成功','slide/index');
            }
            else{
                return  $this->error('失败，请重新操作');
            }
    }

    public function update($sid)
    {
        $res = Db::name('slide')->where('sid',$sid)->find();
        // dump($res);
        // exit;
        $this->assign('res',$res);
        return $this->fetch();
    }

    public function update1($sid)
    {
        $request = Request::instance();
        $post = $request->post();
        // dump($post);
        // exit;
        $file = request()->file('pic');
        if($file)
        {
            $info = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'img');
            if($info)
            {
                $pic = $info->getFilename(); 
            }
        }
        $request = Request::instance();
        $http = $request->domain();
        $src = "$http/zyd/public/static/img/$pic";
        $data=[
            'src'=>$_POST['src'],
            'pic'=> $src,
        ];
        // dump($data);
        // exit;
       $hd = Db::table('slide')->where('sid',$sid)->update($data);
       if($hd)
            {
                return $this->success('修改成功','slide/index');
            }
            else{
                return  $this->error('失败，请重新操作');
            }
    }

    public function delete($sid)
    {
       $hd = Db::table('slide')->delete($sid);
        if($hd)
             {
                 return $this->success('删除成功','slide/index');
             }
             else{
                 return  $this->error('失败，请重新操作');
             }
    }
}
